<?php
class ajax_model extends Model
{
	function ajax_model()
	{
		 parent::Model();
	}
  
	function getAllDetail($num,$offset)
	{
		 $this->db->order_by('id','DESC');
		 $query = $this->db->get('detail',$num,$offset);
		 return $query->result();
	}
	
	function get_total_detail()
	{
		$query = $this->db->get('detail');
		 return $query->num_rows();
	}
	
	function get_last_order()
	{
		 $this->db->order_by('CartID','DESC');
		 $this->db->where('Status !=','Status');
		 $this->db->where('IsDelete',0);
		 return $this->db->get('shoppingcart',5,0)->result();
	}
	/****
	* Thong bao chuyen khoan moi nhat
	*/
	function get_transfer_notice_last()
	{
		 $this->db->select('account.*,transfernotice.*');
		 $this->db->join('account','account.AccountID = transfernotice.AccountID');
		 $this->db->order_by('transfernotice.TransferID','DESC');
		 $this->db->limit('10');
		 return $this->db->get('transfernotice')->result();
	}

	/*****
	* Tinh tong san pham trong moi don hang
	* 
	* @param mixed $CartID
	*/
	function get_sum_product($CartID)
	{
		 $this->db->select_sum('Quantity','total');
		 $this->db->from('cartdetail');
		 $this->db->where('CartID',$CartID);
		 $query = $this->db->get();
		 return $query->row()->total;
	}
	/********
	* Tinh tong tien trong moi don hang
	* 
	* @param mixed $CartID
	*/
	function get_sum_price($CartID)
	{
		  $this->db->where('CartID',$CartID);
		  $list = $this->db->get('cartdetail')->result();
		  $total = 0;
		  foreach($list as $rs):
				 $total = $total + ($rs->Price * $rs->Quantity);
		  endforeach;
		 return $total;
	}  
	
/********* Upload img **********/ 
		

function add_img($filename,$ProductID,$session)
{
  
	if($ProductID!=0)
	{
		if($filename!='')
		{
			fn_resize_image('../uploads/images/'.$filename,'../uploads/product/200/'.$filename,200,200);
			fn_resize_image('../uploads/images/'.$filename,'../uploads/product/80/'.$filename,80,80);
			fn_resize_image('../uploads/images/'.$filename,'../uploads/product/40/'.$filename,40,40);
			fn_resize_image('../uploads/images/'.$filename,'../uploads/product/30/'.$filename,30,30);
			
			//Doan nay lam meo hinh
			/*$this->ResizeImages('../uploads/images/'.$filename,'../uploads/product/200/'.$filename,200,200) ;
			$this->ResizeImages('../uploads/images/'.$filename,'../uploads/product/80/'.$filename,80,80) ;
			$this->ResizeImages('../uploads/images/'.$filename,'../uploads/product/40/'.$filename,40,40) ;
			$this->ResizeImages('../uploads/images/'.$filename,'../uploads/product/30/'.$filename,30,30) ;*/
		}  
	}  
	$data = array(
		'ImagePath' => $filename,
		'ProductID' => (int)$ProductID,
		'ImageSession' => ($ProductID!=0)? '' : $session
	);
	
	$this->db->insert('productimage',$data);
	
	return $this->db->insert_id();
}

function ResizeImages($src, $dest, $w, $h)
{
	//include('SimpleImage.php');
	require_once APPEXCEL.'excel/SimpleImage.php';
	$image = new SimpleImage();
	$image->load($src);
	$image->resize($w,$h);
	$image->save($dest);
}

}
?>
